package com.buaa.utility.services;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;


public class DataSourceDBService extends DBService {
	
	private boolean isDebug = true;
	private String username = null;
	private String password = null;
	private String url = null;
	private String driver = null;
	private int flag;

	public DataSourceDBService(String username, String password, String url,
			String driver) {
		this.username = username;
		this.password = password;
		this.url = url;
		this.driver = driver;
		
		flag = 0;
		if(this.driver!=null){
			if(driver.indexOf("mysql")>=0)
				flag = FLAG_IS_MYSQL;
		}
		
	}

	public DataSource setupDataSource() {
		BasicDataSource ds = new BasicDataSource();
		System.out.println("===driver=============="+driver);
		System.out.println("===username=============="+username);
		System.out.println("===password=============="+password);
		System.out.println("===url=============="+url);
		ds.setDriverClassName(driver);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setUrl(url);
//		ds.setMaxActive(1000);
//		ds.setMaxIdle(30);
//		ds.setMinIdle(10);
		return ds;
	}

	@Override
	public List<Map<String,String>> query(String sql) throws SQLException{
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		DataSource dataSource = setupDataSource(); 
		Connection conn = null;
		Statement stmt = null;
		ResultSet rset = null;
		try {
			long s = System.currentTimeMillis();
			conn = dataSource.getConnection();
			stmt = conn.createStatement();
			if(isDebug) System.out.println("------------DataSourceDBService.query:"+(System.currentTimeMillis()-s)+"ms\n"+sql);
			rset = stmt.executeQuery(sql);
			int numcols = rset.getMetaData().getColumnCount();
			String[] cols = new String[numcols];
			for (int i = 1; i <= numcols; i++) {
				cols[i-1] = rset.getMetaData().getColumnName(i).toLowerCase();
			}
			
			while (rset.next()) {
				Map map = null;
				map = new HashMap();
				for (int i = 0; i < numcols; i++) {
					String value = rset.getString(cols[i]);
					if(value!=null)
						value = value.trim();
					map.put(cols[i],value);
				}
				list.add(map);
			}
			
			if(isDebug) System.out.println("------------DataSourceDBService.query:"+(System.currentTimeMillis()-s)+"ms\n"+sql);
		} catch (SQLException e) {
			throw e;
		} finally {
			try {
				if (rset != null)
					rset.close();
			} catch (Exception e) {
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
			try {
				if (conn != null)
					conn.close();
			} catch (Exception e) {
			}
		}
		return list;
	}

	@Override
	public boolean update(String sql) {
		boolean bool = true;
		DataSource dataSource = setupDataSource(); 
		Connection conn = null;
		Statement stmt = null;
		int num = 0;
		try {
			conn = dataSource.getConnection();
			stmt = conn.createStatement();
			if(isDebug) System.out.println("------------DataSourceDBService.update:\nDataSourceDBService:"+sql);
			num = stmt.executeUpdate(sql);
			System.out.println("num:"+num);
			if(num>=0){
				bool = true;
			}else{
				bool = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			bool = false;
		} finally {
			try {
				if (stmt != null)
					stmt.close();
			} catch (Exception e) {
			}
			try {
				if (conn != null)
					conn.close();
			} catch (Exception e) {
			}
		}
		return bool;
	}
	
	public int getFlag(){
		return flag;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}
